<template>
    <a-row id="globalHeader" align="center" :wrap="false">
        <a-col flex="auto">
            <a-menu mode="horizontal" :selected-keys="selectedKeys" @menu-item-click="doMenuClick">
                <a-menu-item key="0" :style="{ padding: 0, marginRight: '38px' }" disabled>
                    <div class="title-bar">
                        <img class="logo" src="../assets/oj-logo.png" />
                        <div class="title">在线判题系统</div>
                    </div>
                </a-menu-item>
                <a-menu-item v-for="item in visibleRoutes" :key="item.path">
                    {{ item.name }}
                </a-menu-item>
            </a-menu>
        </a-col>
        <a-col flex="100px">
            <div id="userDiv">
                <a-avatar :size="50">
                    {{ store.state.user?.loginUser?.nickname }}
                </a-avatar>
                <a-button type="outline" class="userlog" @click="changeLoginUser(store.state.user?.loginUser?.userRole === ACCESS_ENUM.NOT_LOGIN)">
                    {{ store.state.user?.loginUser?.userRole === ACCESS_ENUM.NOT_LOGIN ? "点击登录" : "退出登录" }}
                </a-button>
            </div>
        </a-col>
    </a-row>
</template>
  
<script setup lang="ts">
import { routes } from "../router/routes";
import { useRoute, useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/accessEnum";

const router = useRouter();
const store = useStore();

// 展示在菜单的路由数组
const visibleRoutes = computed(() => {
    return routes.filter((item, index) => {
        if (item.meta?.hideInMenu) {
            return false;
        }
        // 根据权限过滤菜单
        if (
            !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
        ) {
            return false;
        }
        return true;
    });
});

// 默认主页
const selectedKeys = ref(["/"]);

// 路由跳转后，更新选中的菜单项
router.afterEach((to, from, failure) => {
    selectedKeys.value = [to.path];
});


// setTimeout(() => {
//     console.log(store.state.user?.loginUser);
//     store.dispatch("user/getLoginUser", {
//         userName: "鱼皮管理员",
//         userRole: ACCESS_ENUM.ADMIN,
//     });
// }, 3000);

const changeLoginUser = (f: boolean) => {
    if(f){
        toLogin();
    }else{
        toLogout();
    }
}

const toLogin = () => {
    
    router.push({
        path: `/user/login`,
    });
}

const toLogout = () => {
    store.state.user.loginUser.nickname = "未登录";
    store.state.user.loginUser.id = null;
    store.state.user.loginUser.userRole = ACCESS_ENUM.NOT_LOGIN;
}

const doMenuClick = (key: string) => {
    router.push({
        path: key,
    });
};
</script>
  
<style scoped>
.title-bar {
    display: flex;
    align-items: center;
}

.title {
    color: #444;
    margin-left: 16px;
}

.logo {
    height: 48px;
}

#userDiv {
    height: 50px;
    width: 200px;
}

#userDiv .userlog {
    margin-left: 10%;
}
</style>
  